package applet;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;

public class TimeSelectionDialog extends JDialog implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int YEARS_BACK = 5;
	private static final int LABEL_WIDTH = 100;
	private static final int BUTTON_WIDTH = 100;
	
	public static final int CANCEL_OPTION = 0;
	public static final int OK_OPTION = 1;

	private static String[] months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	private static Integer[] hours = {1,2,3,4,5,6,7,8,9,10,11,12};
	private static String[] minutes = {"00","15","30","45"};
	private static String[] ampm = {"am","pm"};
	
	private JSpinner yearSpinner;
	private CustomJComboBox startMonthCB;
	private CustomJComboBox startDayCB;
	private CustomJComboBox startYearCB;
	private CustomJComboBox startHourCB;
	private CustomJComboBox startMinuteCB;
	private CustomJComboBox startAmpmCB;
	private CustomJComboBox endMonthCB;
	private CustomJComboBox endDayCB;
	private CustomJComboBox endYearCB;
	private CustomJComboBox endHourCB;
	private CustomJComboBox endMinuteCB;
	private CustomJComboBox endAmpmCB;
	private CustomJButton okButton;
	private CustomJButton cancelButton;
	
	private Calendar startTime;
	private Calendar endTime;
	private int selection; 
	
	public TimeSelectionDialog(Window parent, String timeScale, Calendar currentStartTime, Calendar currentEndTime) {
		super(parent, Dialog.ModalityType.APPLICATION_MODAL);
		
		setSize(300, 150);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());

		selection = CANCEL_OPTION;
		
		Box vBox = Box.createVerticalBox();
		
		Calendar today = Calendar.getInstance();
		Date startDate = (new GregorianCalendar(today.get(Calendar.YEAR)-5, 0, 1)).getTime();
		Date endDate = Calendar.getInstance().getTime();
		Integer[] years = new Integer[5];
		for(int i = 0; i < 5; i++) {
			years[i] = today.get(Calendar.YEAR)-YEARS_BACK+i+1;
		}
		
		//yearSpinner = new CustomJSpinner(new SpinnerDateModel(currentTime.getTime(), startDate, endDate, Calendar.YEAR));
		vBox.add(Box.createVerticalStrut(10));
		Box startDropdownBox = Box.createHorizontalBox();
		startDropdownBox.add(Box.createHorizontalStrut(10));
		startDropdownBox.add(new CustomJLabel("Start Time:"));
		startDropdownBox.add(Box.createHorizontalStrut(10));
		startMonthCB = new CustomJComboBox(months);
		startMonthCB.setSelectedIndex(currentStartTime.get(Calendar.MONTH));
		startDropdownBox.add(startMonthCB);
		startDropdownBox.add(Box.createHorizontalStrut(10));
		Integer[] startDays = getDaysForMonth(currentStartTime.get(Calendar.MONTH), currentStartTime.get(Calendar.YEAR));
		startDayCB = new CustomJComboBox(startDays);
		startDayCB.setSelectedIndex(currentStartTime.get(Calendar.DAY_OF_MONTH)-1);
		startDropdownBox.add(startDayCB);
		startDropdownBox.add(Box.createHorizontalStrut(10));
		startYearCB = new CustomJComboBox(years);
		startYearCB.setSelectedIndex(currentStartTime.get(Calendar.YEAR)-today.get(Calendar.YEAR)+YEARS_BACK-1);
		startDropdownBox.add(startYearCB);
		startDropdownBox.add(Box.createHorizontalStrut(10));
		startHourCB = new CustomJComboBox(hours);
		startHourCB.setSelectedIndex((currentStartTime.get(Calendar.HOUR)+11)%12);
		startDropdownBox.add(startHourCB);
		startDropdownBox.add(Box.createHorizontalStrut(10));
		startMinuteCB = new CustomJComboBox(minutes);
		startMinuteCB.setSelectedIndex(currentStartTime.get(Calendar.MINUTE)/15);
		startDropdownBox.add(startMinuteCB);
		startDropdownBox.add(Box.createHorizontalStrut(10));
		startAmpmCB = new CustomJComboBox(ampm);
		startAmpmCB.setSelectedIndex(currentStartTime.get(Calendar.AM_PM));
		startDropdownBox.add(startAmpmCB);
		startDropdownBox.add(Box.createHorizontalStrut(10));
		startMonthCB.addActionListener(this);
		startYearCB.addActionListener(this);
		vBox.add(startDropdownBox);

		vBox.add(Box.createVerticalStrut(10));
		Box endDropdownBox = Box.createHorizontalBox();
		endDropdownBox.add(Box.createHorizontalStrut(10));
		endDropdownBox.add(new CustomJLabel("End Time:"));
		endDropdownBox.add(Box.createHorizontalStrut(10));
		endMonthCB = new CustomJComboBox(months);
		endMonthCB.setSelectedIndex(currentEndTime.get(Calendar.MONTH));
		endDropdownBox.add(endMonthCB);
		endDropdownBox.add(Box.createHorizontalStrut(10));
		Integer[] endDays = getDaysForMonth(currentEndTime.get(Calendar.MONTH), currentEndTime.get(Calendar.YEAR));
		endDayCB = new CustomJComboBox(endDays);
		endDayCB.setSelectedIndex(currentEndTime.get(Calendar.DAY_OF_MONTH)-1);
		endDropdownBox.add(endDayCB);
		endDropdownBox.add(Box.createHorizontalStrut(10));
		endYearCB = new CustomJComboBox(years);
		endYearCB.setSelectedIndex(currentEndTime.get(Calendar.YEAR)-today.get(Calendar.YEAR)+YEARS_BACK-1);
		endDropdownBox.add(endYearCB);
		endDropdownBox.add(Box.createHorizontalStrut(10));
		endHourCB = new CustomJComboBox(hours);
		endHourCB.setSelectedIndex((currentEndTime.get(Calendar.HOUR)+11)%12);
		endDropdownBox.add(endHourCB);
		endDropdownBox.add(Box.createHorizontalStrut(10));
		endMinuteCB = new CustomJComboBox(minutes);
		endMinuteCB.setSelectedIndex(currentEndTime.get(Calendar.MINUTE)/15);
		endDropdownBox.add(endMinuteCB);
		endDropdownBox.add(Box.createHorizontalStrut(10));
		endAmpmCB = new CustomJComboBox(ampm);
		endAmpmCB.setSelectedIndex(currentEndTime.get(Calendar.AM_PM));
		endDropdownBox.add(endAmpmCB);
		endDropdownBox.add(Box.createHorizontalStrut(10));
		endMonthCB.addActionListener(this);
		endYearCB.addActionListener(this);
		vBox.add(endDropdownBox);
		
		vBox.add(Box.createVerticalStrut(10));
		
		Box hBox = Box.createHorizontalBox();
		hBox.add(Box.createHorizontalGlue());
		okButton = new CustomJButton("Ok");
		hBox.add(okButton);
		hBox.add(Box.createHorizontalStrut(10));
		cancelButton = new CustomJButton("Cancel");
		hBox.add(cancelButton);
		hBox.add(Box.createHorizontalStrut(10));
		vBox.add(hBox);
		vBox.add(Box.createVerticalStrut(10));
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		
		add(vBox, BorderLayout.CENTER);
		pack();
		setBounds(parent.getBounds().x+(parent.getBounds().width-getWidth())/2, parent.getBounds().y+(parent.getBounds().height-getHeight())/2, getWidth(), getHeight());
	}
	
	private Integer[] getDaysForMonth(int month, int year) {
		Calendar c = new GregorianCalendar(year, month, 1);
		c.add(Calendar.MONTH, 1);
		c.add(Calendar.DAY_OF_MONTH, -1);
		int daysInMonth = c.get(Calendar.DAY_OF_MONTH);
		Integer[] ret = new Integer[daysInMonth];
		for(int i = 0; i < daysInMonth; i++) ret[i] = i+1;
		return ret;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource() == startMonthCB || arg0.getSource() == startYearCB) {
			Calendar today = Calendar.getInstance();
			int year = startYearCB.getSelectedIndex() + today.get(Calendar.YEAR) - YEARS_BACK + 1;
			int month = startMonthCB.getSelectedIndex();
			int day = startDayCB.getSelectedIndex();
			Integer[] days = getDaysForMonth(month, year);
			startDayCB.setModel(new DefaultComboBoxModel(days));
			if(day > days.length-1) day = days.length-1;
			startDayCB.setSelectedIndex(day);
		}
		else if(arg0.getSource() == endMonthCB || arg0.getSource() == endYearCB) {
			Calendar today = Calendar.getInstance();
			int year = endYearCB.getSelectedIndex() + today.get(Calendar.YEAR) - YEARS_BACK + 1;
			int month = endMonthCB.getSelectedIndex();
			int day = endDayCB.getSelectedIndex();
			Integer[] days = getDaysForMonth(month, year);
			endDayCB.setModel(new DefaultComboBoxModel(days));
			if(day > days.length-1) day = days.length-1;
			endDayCB.setSelectedIndex(day);
		}
		else if(arg0.getSource() == cancelButton) {
			this.setVisible(false);
			this.dispose();
		}
		else if(arg0.getSource() == okButton) {
			Calendar newStart = Calendar.getInstance();
			Calendar newEnd = Calendar.getInstance();
			Calendar today = Calendar.getInstance();
			newStart.set(Calendar.YEAR, startYearCB.getSelectedIndex() + today.get(Calendar.YEAR) - YEARS_BACK + 1);
			newStart.set(Calendar.MONTH, startMonthCB.getSelectedIndex());
			newStart.set(Calendar.DAY_OF_MONTH, startDayCB.getSelectedIndex()+1);
			newStart.set(Calendar.HOUR, (startHourCB.getSelectedIndex()+1)%12);
			newStart.set(Calendar.MINUTE, startMinuteCB.getSelectedIndex()*15);
			newStart.set(Calendar.AM_PM, startAmpmCB.getSelectedIndex());
			newEnd.set(Calendar.YEAR, endYearCB.getSelectedIndex() + today.get(Calendar.YEAR) - YEARS_BACK + 1);
			newEnd.set(Calendar.MONTH, endMonthCB.getSelectedIndex());
			newEnd.set(Calendar.DAY_OF_MONTH, endDayCB.getSelectedIndex()+1);
			newEnd.set(Calendar.HOUR, (endHourCB.getSelectedIndex()+1)%12);
			newEnd.set(Calendar.MINUTE, endMinuteCB.getSelectedIndex()*15);
			newEnd.set(Calendar.AM_PM, endAmpmCB.getSelectedIndex());
			if(newStart.after(today)) {
				MessageDialog message = new MessageDialog(this, "Start time cannot be in the future");
				message.setVisible(true);
			}
			else if(newEnd.after(today)) {
				MessageDialog message = new MessageDialog(this, "End time cannot be in the future");
				message.setVisible(true);
			}
			else if(newStart.after(newEnd)) {
				MessageDialog message = new MessageDialog(this, "Start time must be before end time");
				message.setVisible(true);
			}
			//everything okay
			else {
				selection = OK_OPTION;
				startTime = (Calendar)newStart.clone();
				endTime = (Calendar)newEnd.clone();
				this.setVisible(false);
				this.dispose();
			}
		}
	}
	
	public int getSelection() {
		return selection;
	}
	
	public Calendar getStartTime() {
		return startTime;
	}
	
	public Calendar getEndTime() {
		return endTime;
	}

	private class CustomJLabel extends JLabel {
		public CustomJLabel(String label) {
			super(label);
			setFont(DisplayApplet.customFont);
		}
		@Override
		public Dimension getPreferredSize() {
			return new Dimension(LABEL_WIDTH, DisplayApplet.ITEM_BUTTON_HEIGHT);
		}
		@Override
		public Dimension getMinimumSize() {
			return new Dimension(LABEL_WIDTH, DisplayApplet.ITEM_BUTTON_HEIGHT);
		}
		@Override
		public Dimension getMaximumSize() {
			return new Dimension(LABEL_WIDTH, DisplayApplet.ITEM_BUTTON_HEIGHT);
		}
	}

	private class CustomJButton extends JButton {
		public CustomJButton(String s) {
			super(s);
			setFont(DisplayApplet.customFont);
		}
		@Override
		public Dimension getPreferredSize() {
			//int width = super.getPreferredSize().width;
			return new Dimension(BUTTON_WIDTH, DisplayApplet.ITEM_BUTTON_HEIGHT);
		}
		@Override
		public Dimension getMinimumSize() {
			//int width = super.getMinimumSize().width;
			return new Dimension(BUTTON_WIDTH, DisplayApplet.ITEM_BUTTON_HEIGHT);
		}
		@Override
		public Dimension getMaximumSize() {
			//int width = super.getMaximumSize().width;
			return new Dimension(BUTTON_WIDTH, DisplayApplet.ITEM_BUTTON_HEIGHT);
		}
	}

	private class CustomJComboBox extends JComboBox {
		public CustomJComboBox(Object[] objects) {
			super(objects);
			setFont(DisplayApplet.customFont);
		}
		@Override
		public Dimension getPreferredSize() {
			int width = super.getPreferredSize().width;
			return new Dimension(width, DisplayApplet.ITEM_BUTTON_HEIGHT-20);
		}
		@Override
		public Dimension getMinimumSize() {
			int width = super.getMinimumSize().width;
			return new Dimension(width, DisplayApplet.ITEM_BUTTON_HEIGHT-20);
		}
		@Override
		public Dimension getMaximumSize() {
			int width = super.getMaximumSize().width;
			return new Dimension(width, DisplayApplet.ITEM_BUTTON_HEIGHT-20);
		}
	}

}
